package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResAppStartUpBean;
import com.rjwh.dingdong.client.bean.jsonbean.ResSysLogin;
import com.rjwh.dingdong.client.bean.localbean.AppStartUpAuxiliary;
import com.rjwh.dingdong.client.bean.localbean.AppStartUpData;
import com.rjwh.dingdong.client.bean.localbean.SysLoginBabylistBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginDataBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginUserprofileBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginValidityBean;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.SplashModel;
import com.rjwh.dingdong.client.mvp.presenter.ISplashPresenter;
import com.rjwh.dingdong.client.mvp.view.SplashView;
import com.rjwh.dingdong.client.network.ApiRequest;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author 王小珂
 * @ClassName SplashPresenter
 * @Description
 * @date 2017-1-13
 * @history 2017-1-13 author: description:
 */

public class SplashPresenter extends BasePresenter<SplashView> implements ISplashPresenter{

    private SplashModel model;

    public SplashPresenter(Context context, SplashView mMvpView) {
        super(context, mMvpView);
        model = new SplashModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {

                case NetConstant.APPSTARTUP:
                    ResAppStartUpBean resAppStartUpBean = (ResAppStartUpBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resAppStartUpBean != null) {
                        AppStartUpData data = resAppStartUpBean.getData();
                        if (data != null) {
                            AppStartUpAuxiliary auxiliary = data.getAuxiliary();
                            SysLoginUserprofileBean userprofile = data.getUserprofile();
                            SysLoginValidityBean validity = data.getValidity();
                            List<SysLoginBabylistBean> babylist = data.getBabylist();

                            if(auxiliary!=null){
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_APP_STARTUP_BADGE, auxiliary.getBadge());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_APP_STARTUP_URL, auxiliary.getUrl());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_APP_STARTUP_ACTIONURL, auxiliary.getActionurl());
                            }

                            if (userprofile != null) {
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERID, userprofile.getUserid());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERPHONE, userprofile.getPhone());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERNAME, userprofile.getNickname());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERIMAGE, userprofile.getAvatar());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERCOMPLETE, userprofile.getComplete());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERLEVEL, userprofile.getUserlevel());
                            }

                            if (validity != null) {
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USER_TOKEN, validity.getToken());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USER_HASPWD, validity.getHaspwd());
                            }

                            if (babylist != null && !babylist.isEmpty()) {

                                for (SysLoginBabylistBean sysLoginBabylistBean : babylist) {
                                    String chief = sysLoginBabylistBean.getChief();
                                    if (chief.equals("1")) {//取当前绑定的宝宝
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYID, sysLoginBabylistBean.getBabyid());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYNAME, sysLoginBabylistBean.getBabyname());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYSEX, sysLoginBabylistBean.getSex());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYBIRTHDAY, sysLoginBabylistBean.getBirthday());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERCHENGWEI, sysLoginBabylistBean.getTitle());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYAVATAR, sysLoginBabylistBean.getBabyavatar());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYAGE, sysLoginBabylistBean.getBabyage());
                                    }
                                }
                            }

                            try {
                                ApplicationInfo appInfo = getContext().getPackageManager()
                                        .getApplicationInfo(getContext().getPackageName(),
                                                PackageManager.GET_META_DATA);
                                String msg = appInfo.metaData.getString("PUSH_APPID");
                                LogUtil.e("LoginPresenter", "PUSH_APPID:" + msg);
                                if (!AsStrUtils.isEmpty(msg)) {
                                    LogUtil.d("LoginPresenter", "绑定推送");
                                    ApiRequest.pushBindingForAppId(msg, MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_CLIENTID), null);
                                }
                            } catch (PackageManager.NameNotFoundException e) {
                                e.printStackTrace();
                            }
                        }

                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        LogUtil.d(mAsResult.getResultMsg());
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 启动更新数据
     */
    @Override
    public void doAppStartUp() {
        model.doAppStartUp();
    }
}
